package com.os.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.os.dao.StoreDaoH;
import com.os.model.StoreH;
import com.os.util.JdbcConUtilM;
/**
 * 库存表Dao实现类
 * @author HQG 2017年1月4日
 *
 */
public class StoreDaoImplH implements StoreDaoH{
	
	public List<StoreH> getAllFoodsH(){
		List<StoreH> foods = new ArrayList<StoreH>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from o_food");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				StoreH s = new StoreH();
				s.setFid(rs.getInt("fid"));
				s.setFname(rs.getString("fname"));
				foods.add(s);
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return foods;
		
	}

	public boolean setStoresH(List<StoreH> foods){
		boolean falg = false;
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO o_store(fid,fname)");
		sql.append(" VALUES (?,?)");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			for (int i = 0; i < foods.size(); i++) {
				ps.setInt(1, foods.get(i).getFid());
				ps.setString(2, foods.get(i).getFname());
				int a = ps.executeUpdate();
				if (a>0) {
					falg = true;
				}else{
					falg = false;
					break;
				}
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return falg;
	}
	
	@Override
	public List<StoreH> getLoadStoresH(int page, int rows) {
		List<StoreH> stores = new ArrayList<StoreH>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from o_store");
		sql.append(" limit ?,?");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ps.setInt(1, (page-1)*rows);
			ps.setInt(2, rows);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				StoreH s = new StoreH();
				s.setFid(rs.getInt("fid"));
				s.setFname(rs.getString("fname"));
				s.setFnums(rs.getInt("fnums"));
				stores.add(s);
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stores;
	}

	@Override
	public int getAllStoresH() {
		int count = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("select count(fid) scount");
		sql.append(" from o_store");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				count=rs.getInt("scount");
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public int updateStoreH(int fid, int fnums) {
		int falg = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE o_store");
		sql.append(" set fnums=?");
		sql.append(" where fid=?");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ps.setInt(1, fnums);
			ps.setInt(2, fid);
			falg = ps.executeUpdate();
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return falg;
	}

	@Override
	public int deleteStoreAllH() {
		int falg = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE FROM o_store");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			falg = ps.executeUpdate();
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return falg;
	}

	@Override
	public List<StoreH> findStoresByFnameH(String fname) {
		List<StoreH> stores = new ArrayList<StoreH>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from o_store");
		sql.append(" where fname like ?");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ps.setString(1, "%"+fname+"%");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				StoreH s = new StoreH();
				s.setFid(rs.getInt("fid"));
				s.setFname(rs.getString("fname"));
				s.setFnums(rs.getInt("fnums"));
				stores.add(s);
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stores;
	}

	@Override
	public int getFnumsByFid(int fid) {
		int fnums = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("select fnums");
		sql.append(" from o_store");
		sql.append(" where fid=?");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ps.setInt(1, fid);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				fnums = rs.getInt("fnums");
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fnums;
	}

	@Override
	public boolean addStoreH(int fid, String fname) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO o_store(fid,fname)");
		sql.append(" VALUES (?,?)");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ps.setInt(1, fid);
			ps.setString(2, fname);
			int a = ps.executeUpdate();
			if(a != 0){
				flag = true;
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean updateStoreH(int fid, String fname) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE o_store SET fname=?");
		sql.append(" where fid=?");
		Connection con = JdbcConUtilM.getCon();
		try {
			PreparedStatement ps = con.prepareStatement(sql.toString());
			ps.setString(1, fname);
			ps.setInt(2, fid);
			int a = ps.executeUpdate();
			if(a != 0){
				flag = true;
			}
			JdbcConUtilM.closePstAndCon(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	
}
